package entities;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.CollectionOfElements;

import entities.vo.ArticuloVO;
import entities.vo.HogarVO;
import entities.vo.OFADVO;
import entities.vo.RopaVO;

@Entity
@IdClass(IdOFAD.class)
public class OFAD implements Serializable {
	private static final long serialVersionUID = 1L;
	
    @Id
	@AttributeOverrides({
	@AttributeOverride(name = "nroOfad",
	column = @Column(name="nroOfad")),
	@AttributeOverride(name = "idCasaCentral",
	column = @Column(name="idCasaCentral"))
	})
	
	private long nroOfad;
    private long idCasaCentral;
    private long fecha;
    @CollectionOfElements
    List<ItemOFAD> items;

    public OFAD() {
    	items = new LinkedList<ItemOFAD>();
    }
    
    @OneToMany(/*mappedBy="ofad",*/targetEntity=entities.ItemOFAD.class, fetch=FetchType.EAGER)
	@JoinTable(name="OFAD_ItemOFAD")
    public List<ItemOFAD> getItems() {
    	return items;
    }
    
    public void setItems(List<ItemOFAD> items) {
    	this.items = items;
    }
    
    public void addItem(ItemOFAD item){
    	items.add(item);
    }
    
    public long getFecha() {
		return fecha;
	}
    public void setFecha(long fecha) {
		this.fecha = fecha;
	}
    
    @Transient
    public OFADVO getOFADVO(){
    	OFADVO ofadvo = new OFADVO();
    	ofadvo.setNroOfad(getNroOfad());
    	ofadvo.setIdCasaCentral(getIdCasaCentral());
    	for (ItemOFAD item : getItems()) {    		
			ofadvo.addArticulo(item.getArticulo().getArticuloVO());
		}
    	return ofadvo;
    }
    public void setOFADVO(OFADVO ofadVO){
    	if(ofadVO!=null){
	    	setNroOfad(ofadVO.getNroOfad());
	    	setIdCasaCentral(ofadVO.getIdCasaCentral());
	    	for (ArticuloVO articuloVo : ofadVO.getArticulos()) {
				Articulo articulo = null;
				if(articuloVo instanceof RopaVO){
					articulo = new ArticuloRopa();
				}else if(articuloVo instanceof HogarVO){
					articulo = new ArticuloHogar();
				}
				articulo.setArticuloVO(articuloVo);
				addItem(new ItemOFAD(/*this,*/articulo));
			}
    	}
    }

	private Articulo ArticuloHogar() {
		// TODO Auto-generated method stub
		return null;
	}

	private Articulo ArticuloRopa() {
		// TODO Auto-generated method stub
		return null;
	}

	public long getNroOfad() {
		return nroOfad;
	}

	public void setNroOfad(long nroOfad) {
		this.nroOfad = nroOfad;
	}

	public long getIdCasaCentral() {
		return idCasaCentral;
	}

	public void setIdCasaCentral(long idCasaCentral) {
		this.idCasaCentral = idCasaCentral;
	}

}
